<?php
class Application_Model_Members
{
	private $objDb, $m_cache;
	private static $_instance = null;

	public function __construct()
	{
		$this->objDb = Zend_Registry::get('db');
		$this->m_cache = Zend_Registry::get('cache');
	}

	public static function getInstance()
	{
		if (null === self::$_instance) self::$_instance = new self();
		return self::$_instance;
	}

	/**
	 * Insert member
	 * @author	Viet <quocviet1501@gmail.com>
	 * @date	Oct 16, 2013
	 * @param	array
	 * @return	member_id
	 */
	public function insertMember($p_arrParam)
	{
		$stmt = $this->objDb->prepare('CALL members_insert(:p_member_email,:p_member_password,:p_member_firstname,:p_member_lastname,:p_location_id,:p_indistry_id,:p_member_address,:p_member_birthday,:p_member_phone,:p_level_id,:p_degree_id,:p_member_merritial,:p_member_gender,:p_member_language_level,:p_member_picture,@p_return);');
		$stmt -> bindParam('p_member_email', 			$p_arrParam['member_email'], 			PDO::PARAM_STR);
		$stmt -> bindParam('p_member_password', 		$p_arrParam['member_password'], 		PDO::PARAM_STR);
		$stmt -> bindParam('p_member_firstname', 		$p_arrParam['member_firstname'], 		PDO::PARAM_STR);
		$stmt -> bindParam('p_member_lastname', 		$p_arrParam['member_lastname'], 		PDO::PARAM_STR);
		$stmt -> bindParam('p_location_id', 			$p_arrParam['location_id'], 			PDO::PARAM_INT);
		$stmt -> bindParam('p_indistry_id', 			$p_arrParam['indistry_id'], 			PDO::PARAM_INT);
		$stmt -> bindParam('p_member_address', 			$p_arrParam['member_address'], 			PDO::PARAM_STR);
		$stmt -> bindParam('p_member_birthday', 		$p_arrParam['member_birthday'], 		PDO::PARAM_STR);
		$stmt -> bindParam('p_member_phone', 			$p_arrParam['member_phone'], 			PDO::PARAM_STR);
		$stmt -> bindParam('p_level_id', 				$p_arrParam['level_id'], 				PDO::PARAM_INT);
		$stmt -> bindParam('p_degree_id', 				$p_arrParam['degree_id'], 				PDO::PARAM_INT);
		$stmt -> bindParam('p_member_merritial', 		$p_arrParam['member_merritial'], 		PDO::PARAM_INT);
		$stmt -> bindParam('p_member_gender', 			$p_arrParam['member_gender'], 			PDO::PARAM_INT);
		$stmt -> bindParam('p_member_language_level', 	$p_arrParam['member_language_level'], 	PDO::PARAM_STR);
		$stmt -> bindParam('p_member_picture', 			$p_arrParam['member_picture'], 			PDO::PARAM_STR);
		$stmt -> execute();
		$stmt->closeCursor();
		$intResult = $this->objDb->query('SELECT @p_return')->fetchColumn(0);
		return $intResult;
	}
	
	/**
	 * active member by email
	 * @author	Viet <quocviet1501@gmail.com>
	 * @date	Oct 16, 2013
	 * @param	member_email
	 * @return	1:success, 0:fail
	 */
	public function activeMember($p_member_email)
	{
		$stmt = $this->objDb->prepare('CALL members_active(:p_member_email,@p_return);');
		$stmt -> bindParam('p_member_email', $p_member_email, PDO::PARAM_STR);
		$stmt -> execute();
		$stmt->closeCursor();
		$intResult = $this->objDb->query('SELECT @p_return')->fetchColumn(0);
		return $intResult;
	}
	/**
	 * check member email by email
	 * @author	Viet <quocviet1501@gmail.com>
	 * @date	Oct 16, 2013
	 * @param	member_email
	 * @return	1:exist, 0:not exist
	 */
	public function checkMemberEmail($p_member_email)
	{
		$stmt = $this->objDb->prepare('CALL members_check_email(:p_member_email);');
		$stmt -> bindParam('p_member_email', $p_member_email, PDO::PARAM_STR);
		$stmt -> execute();
		$arrResults  = $stmt->fetchAll();
		$stmt->closeCursor();
		return $arrResults[0]['number'];
	}
	/**
	 * get detail member by email
	 * @author	Viet <quocviet1501@gmail.com>
	 * @date	Oct 16, 2013
	 * @param	member_email
	 * @return	1:exist, 0:not exist
	 */
	public function getDetailMember($p_member_email)
	{
		$stmt = $this->objDb->prepare('CALL members_getdetail(:p_member_email);');
		$stmt -> bindParam('p_member_email', $p_member_email, PDO::PARAM_STR);
		$stmt -> execute();
		$arrResults  = $stmt->fetchAll();
		$stmt->closeCursor();
		return $arrResults;
	}
	/**
	 * check login
	 * @funtion	loginMember
	 * @author	Viet <quocviet1501@gmail.com>
	 * @date	Oct 16, 2013
	 * @param	array
	 * @return	array
	 */
	public function loginMember($p_arrParam)
	{
		$stmt = $this->objDb->prepare('CALL members_login(:p_member_email,:p_member_password);');
		$stmt -> bindParam('p_member_email', 		$p_arrParam['member_email'], 	PDO::PARAM_STR);
		$stmt -> bindParam('p_member_password', 	$p_arrParam['member_password'], PDO::PARAM_STR);
		$stmt -> execute();
		$arrResults  = $stmt->fetchAll();
		$stmt->closeCursor();
		return $arrResults;
	}
	/**
	 * update password member
	 * @funtion	updatePasswordMember
	 * @author	Viet <quocviet1501@gmail.com>
	 * @date	Oct 16, 2013
	 * @param	array
	 * @return	1:success,0:fail
	 */
	public function updatePasswordMember($p_arrParam)
	{
		$stmt = $this->objDb->prepare('CALL members_update_password(:p_member_email,:p_member_password,@p_return);');
		$stmt -> bindParam('p_member_email', 		$p_arrParam['member_email'], 	PDO::PARAM_STR);
		$stmt -> bindParam('p_member_password', 	$p_arrParam['member_password'], PDO::PARAM_STR);
		$stmt -> execute();
		$stmt->closeCursor();
		$intResult = $this->objDb->query('SELECT @p_return')->fetchColumn(0);
		return $intResult;
	}
	/**
	 * update member
	 * @author	Viet <quocviet1501@gmail.com>
	 * @date	Oct 16, 2013
	 * @param	array
	 * @return	1:success,0:fail
	 */
	public function updateMember($p_arrParam)
	{
		$stmt = $this->objDb->prepare('CALL members_update(:p_member_email,:p_member_password,:p_member_firstname,:p_member_lastname,:p_location_id,:p_indistry_id,:p_member_address,:p_member_birthday,:p_member_phone,:p_level_id,:p_degree_id,:p_member_merritial,:p_member_gender,:p_member_language_level,:p_member_picture,@p_return);');
		$stmt -> bindParam('p_member_email', 			$p_arrParam['member_email'], 			PDO::PARAM_STR);
		$stmt -> bindParam('p_member_password', 		$p_arrParam['member_password'], 		PDO::PARAM_STR);
		$stmt -> bindParam('p_member_firstname', 		$p_arrParam['member_firstname'], 		PDO::PARAM_STR);
		$stmt -> bindParam('p_member_lastname', 		$p_arrParam['member_lastname'], 		PDO::PARAM_STR);
		$stmt -> bindParam('p_location_id', 			$p_arrParam['location_id'], 			PDO::PARAM_INT);
		$stmt -> bindParam('p_indistry_id', 			$p_arrParam['indistry_id'], 			PDO::PARAM_INT);
		$stmt -> bindParam('p_member_address', 			$p_arrParam['member_address'], 			PDO::PARAM_STR);
		$stmt -> bindParam('p_member_birthday', 		$p_arrParam['member_birthday'], 		PDO::PARAM_STR);
		$stmt -> bindParam('p_member_phone', 			$p_arrParam['member_phone'], 			PDO::PARAM_STR);
		$stmt -> bindParam('p_level_id', 				$p_arrParam['level_id'], 				PDO::PARAM_INT);
		$stmt -> bindParam('p_degree_id', 				$p_arrParam['degree_id'], 				PDO::PARAM_INT);
		$stmt -> bindParam('p_member_merritial', 		$p_arrParam['member_merritial'], 		PDO::PARAM_INT);
		$stmt -> bindParam('p_member_gender', 			$p_arrParam['member_gender'], 			PDO::PARAM_INT);
		$stmt -> bindParam('p_member_language_level', 	$p_arrParam['member_language_level'], 	PDO::PARAM_STR);
		$stmt -> bindParam('p_member_picture', 			$p_arrParam['member_picture'], 			PDO::PARAM_STR);
		$stmt -> execute();
		$stmt->closeCursor();
		$intResult = $this->objDb->query('SELECT @p_return')->fetchColumn(0);
		return $intResult;
	}
}
?>